package com.stadium.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.stadium.common.BaseResponse;
import com.stadium.common.utils.HttpHelper;
import com.stadium.dto.BasicsRecordDto;
import com.stadium.dto.RecordDictDto;
import com.stadium.dto.RecordStatisticalDto;
import com.stadium.service.RecordStatisticalSV;
import com.stadium.service.impl.RecordExcelExport;
import com.stadium.vo.RecordStatisticalVo;
import com.stadium.vo.searchVo.BasicsRecordSearchVo;
import com.stadium.vo.searchVo.RecordStatisticalSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

/**
 * 比赛事件统计
 *
 * @author wsl
 * @email
 * @date 2021-06-21 22:36:07
 */
@RestController
@RequestMapping("/api/statistical")
@Api(tags = "比赛事件统计")
public class RecordStatisticalCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RecordStatisticalSV recordStatisticalSV;
    @Autowired
    private RecordExcelExport recordExcelExport;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated RecordStatisticalVo recordStatisticalVo) {
        try {
            if (recordStatisticalVo.getId() == null ||recordStatisticalVo.getId() == ""){
                    recordStatisticalSV.save(recordStatisticalVo);
            }else{
                    recordStatisticalSV.update(recordStatisticalVo);
            }
            return BaseResponse.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
            recordStatisticalSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "/v1/deleteBatch")
    @ApiImplicitParams({@ApiImplicitParam(name="ids",value="ids")})
    public BaseResponse deleteBatch(@RequestParam(name = "ids", required = true)String[] ids) {
        try {
            recordStatisticalSV.deleteBatch(ids);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<RecordStatisticalDto>> query(@Validated RecordStatisticalSearchVo searchVo) {
        try {
            List<RecordStatisticalDto> queryList = recordStatisticalSV.query(searchVo);
            int total = recordStatisticalSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "球员/球队统计项", notes = "球员/球队统计项")
    @GetMapping(value = "/v1/getHeader")
    @ApiImplicitParams({@ApiImplicitParam(name="eventId",value="赛事Id"),
            @ApiImplicitParam(name="belong",value="0 个人事件项，1：球队事件项")})
    public BaseResponse<List<RecordDictDto>> getHeader(@RequestParam(name = "eventId", required = true)String eventId,
                                                                      @RequestParam(name = "belong", required = true)String belong) {
        try {
            List<RecordDictDto> queryData = recordStatisticalSV.getHeader(eventId,belong);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "球员/球队/事件统计", notes = "球员/球队/事件统计")
    @GetMapping(value = "/v1/getInfo")
    @ApiImplicitParams({@ApiImplicitParam(name="scheduleId",value="赛程Id"),
            @ApiImplicitParam(name="belong",value="0 个人事件，1：球队事件")})
    public BaseResponse<Map<String,List<Map<String,Object>>>> getInfo(@RequestParam(name = "scheduleId", required = true)String scheduleId,
                                                          @RequestParam(name = "belong", required = true)String belong) {
        try {
            Map<String,List<Map<String,Object>>> queryData = recordStatisticalSV.getInfo(scheduleId,belong);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error(e.getMessage());
        }
    }


    @ApiOperation(value = "事件统计")
    @GetMapping(value = "/v1/record")
    @ApiImplicitParams({@ApiImplicitParam(name="scheduleId",value="赛程Id")})
    public BaseResponse<Map<String,List<BasicsRecordDto>>> record(@RequestParam(name = "scheduleId", required = true)String scheduleId) {
        try {
            Map<String,List<BasicsRecordDto>> queryData = recordStatisticalSV.record(scheduleId);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("事件统计失败！", e);
            return BaseResponse.error("事件统计失败");
        }
    }


    @ApiOperation("导出事件记录")
    @GetMapping("/exportRecordExcel")
    public BaseResponse exportExcelTemplate ( @ApiIgnore HttpServletResponse response,@RequestParam(name = "scheduleId", required = true)String scheduleId){
        try {
            File file = new File("/比赛事件记录统计" + System.currentTimeMillis() + ".xlsx");
            recordExcelExport.createExcel(file,scheduleId);
            HttpHelper.responseFile(response, file, "比赛事件记录统计.xlsx");
            return BaseResponse.ok();
        } catch (Exception e) {
            logger.error("事件统计失败！", e);
            return BaseResponse.error("事件统计失败");
        }
    }

    @ApiOperation("导出比赛统计")
    @GetMapping("/exportStatisticalExcel")
    public BaseResponse exportStatisticalExcel ( @ApiIgnore HttpServletResponse response,@RequestParam(name = "scheduleId", required = true)String scheduleId){
        try {
            File file = new File("/比赛事件记录统计" + System.currentTimeMillis() + ".xlsx");
            String s = recordExcelExport.exportTemplate(file, scheduleId);
            //HttpHelper.responseFile(response, file, "比赛统计.xlsx");
            return BaseResponse.ok(s);
        } catch (Exception e) {
            logger.error("事件统计失败！", e);
            return BaseResponse.error("事件统计失败");
        }
    }

}
